<?php

namespace zhtfrontend\controllers;


use doumiaoduo\buildform\BuildForm;
use zhtfrontend\models\forms\SettingCardtimeForm;
use zhtfrontend\models\forms\SettingCommissionrateForm;
use zhtfrontend\models\forms\SettingDepositForm;
use common\models\datas\Setting;
use common\components\helpers\Fun;
use zhtfrontend\models\forms\SettingRefundForm;
use zhtfrontend\models\forms\SettingScholarshipForm;

class SettingController extends \zhtfrontend\components\bases\BaseController
{

    public function actionIndex()
    {
        $depositModel = new SettingDepositForm();
        $depositData = Setting::findOne(['key' => 'deposit','branch_id'=>Fun::getSiteId()]);
        $depositModel->attributes = !empty($depositData->value) ? unserialize($depositData->value) : null;

        $cardTimeModel=new SettingCardtimeForm();
        $cardTimeData=Setting::findOne(['key' => 'cardtime','branch_id'=>Fun::getSiteId()]);
        $cardTimeModel->attributes = !empty($cardTimeData->value) ? unserialize($cardTimeData->value) : null;

        $scholarshipModel=new SettingScholarshipForm();
        $scholarshipData=Setting::findOne(['key' => 'scholarship','branch_id'=>Fun::getSiteId()]);
        $scholarshipModel->attributes = !empty($scholarshipData->value) ? unserialize($scholarshipData->value) : null;

        $refundModel=new SettingRefundForm();
        $refundData=Setting::findOne(['key' => 'refund','branch_id'=>Fun::getSiteId()]);
        $refundModel->attributes = !empty($refundData->value) ? unserialize($refundData->value) : null;

        $commissionrateModel=new SettingCommissionrateForm();
        $commissionrateData=Setting::findOne(['key' => 'commissionrate','branch_id'=>Fun::getSiteId()]);
        $commissionrateModel->attributes = !empty($commissionrateData->value) ? unserialize($commissionrateData->value) : null;

        $form = new BuildForm('@app/components/forms/settingForm.php');

        $settingDeposit = $depositData != null ? $depositData : new Setting(['key' => 'deposit', 'item' => '定金设置']);
        $settingCardTime = $cardTimeData != null ? $cardTimeData : new Setting(['key' => 'cardtime', 'item' => '卡激活时间限制']);
        $settingScholarship = $scholarshipData != null ? $scholarshipData : new Setting(['key' => 'scholarship', 'item' => '奖学金设置']);
        $settingRefund = $refundData != null ? $refundData : new Setting(['key' => 'refund', 'item' => '订单课程退款比例']);
        $settingCommissionrate = $commissionrateData != null ? $commissionrateData : new Setting(['key' => 'commissionrate', 'item' => '佣金比例']);
        $form->loadModel(['deposit' => $depositModel,'cardtime'=>$cardTimeModel,'scholarship'=>$scholarshipModel,'refund'=>$refundModel,'commissionrate'=>$commissionrateModel]);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
//            $settingDeposit->value = serialize($depositModel->attributes);
//            $settingDeposit->branch_id = Fun::getSiteId();
//            $settingDeposit->user_id = \Yii::$app->user->id;
//
//            $settingCardTime->value = serialize($cardTimeModel->attributes);
//            $settingCardTime->branch_id = Fun::getSiteId();
//            $settingCardTime->user_id =\Yii::$app->user->id;
//
//            $settingScholarship->value = serialize($scholarshipModel->attributes);
//            $settingScholarship->branch_id = Fun::getSiteId();
//            $settingScholarship->user_id =\Yii::$app->user->id;

            $settingRefund->value = serialize($refundModel->attributes);
            $settingRefund->branch_id = Fun::getSiteId();
            $settingRefund->user_id =\Yii::$app->user->id;

            $settingCommissionrate->value = serialize($commissionrateModel->attributes);
            $settingCommissionrate->branch_id = Fun::getSiteId();
            $settingCommissionrate->user_id =\Yii::$app->user->id;

            if ($settingRefund->save(false) && $settingCommissionrate->save(false)) {
                return $this->redirectMessage('设置保存成功', Fun::url(['setting/index']));
            }
        }
        return $this->render('index', ['form' => $form->render(['type'=>'refund'])]);
    }
}
